# !/bin/bash

script_path="${BASH_SOURCE[0]}"
# get script abs directory
while [ -h "$script_path" ]; do
    script_dir="$(cd -P "$(dirname "${script_path}")" >/dev/null 2>&1 && pwd)"
    script_path="$(readlink "$script_path")"
    [[ $script_path != /* ]] && script_path="$script_dir/$script_path"
done
script_dir="$(cd -P "$(dirname "${script_path}")" >/dev/null 2>&1 && pwd)"

project_dir=$(realpath $(dirname "${script_dir}"))
echo -e "Found project DIR_PATH: \033[32m $project_dir \033[0m"

# Constants
third_party_dir="$project_dir/third-party"


# utils functions
function check_git_install() {
    if ! command -v git >/dev/null 2>&1 ; then
        echo -e "\033[31mGit not install \033[0m"
        exit 1
    else
        echo -e "\033[32mGit installed \033[0m"
    fi
}


function check_repo_exists() {
    local repo_dir="$1"
    local repo_url="$2"

    if [ ! -d "$repo_dir" ]; then
        echo -e "Found $repo_dir: \033[31m False \033[0m"
        echo -e "Cloning  : \033[47;34m $repo_url \033[0m to \033[47;34m third-party \033[0m"
        git clone --recursive "$repo_url" "$repo_dir" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo -e "\033[47;31mCan not clone $repo_url, make sure you have the correct access rights.\033[0m"
            return 1
        fi
    else
        echo -e "Found $repo_dir : \033[32m True \033[0m"
    fi
    git config --global --add safe.directory "$repo_dir/*"
    return 0
}


function check_include_lib_exist() {
    local include_dir="$1"
    local lib_file="$2"

    if [ -d "$include_dir" ] && [ -f "$lib_file" ]; then
        echo -e "Found $include_dir and $lib_file : \033[32mTrue\033[0m"
        return 0
    else
        echo -e "Found $include_dir or $lib_file : \033[31mFalse\033[0m"
        return 1
    fi
}